package models

import (
	"time"

	"gorm.io/gorm"
)

// MCPTool MCP工具模型
type MCPTool struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	Name        string         `json:"name" gorm:"not null;uniqueIndex;size:100"`
	Description string         `json:"description" gorm:"size:500"`
	Schema      string         `json:"schema" gorm:"type:jsonb"`         // JSON Schema定义
	Handler     string         `json:"handler" gorm:"not null;size:200"` // 处理器路径或脚本
	Version     string         `json:"version" gorm:"default:1.0.0"`
	Category    string         `json:"category" gorm:"size:50"`
	Tags        string         `json:"tags" gorm:"type:jsonb"`
	IsEnabled   bool           `json:"is_enabled" gorm:"default:true"`
	UsageCount  int64          `json:"usage_count" gorm:"default:0"`
	CreatedBy   uint           `json:"created_by"`
	UpdatedBy   uint           `json:"updated_by"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联
	Executions []MCPExecution `json:"executions,omitempty" gorm:"foreignKey:ToolID"`
}

// MCPResource MCP资源模型
type MCPResource struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	URI         string         `json:"uri" gorm:"not null;uniqueIndex;size:500"` // 资源URI
	Name        string         `json:"name" gorm:"not null;size:200"`
	Description string         `json:"description" gorm:"size:500"`
	MimeType    string         `json:"mime_type" gorm:"size:100"`
	Content     string         `json:"content" gorm:"type:text"`   // 资源内容
	Metadata    string         `json:"metadata" gorm:"type:jsonb"` // 元数据
	Size        int64          `json:"size" gorm:"default:0"`
	Version     string         `json:"version" gorm:"default:1.0.0"`
	IsPublic    bool           `json:"is_public" gorm:"default:false"`
	AccessCount int64          `json:"access_count" gorm:"default:0"`
	CreatedBy   uint           `json:"created_by"`
	UpdatedBy   uint           `json:"updated_by"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`
}

// MCPPrompt MCP提示模板模型
type MCPPrompt struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	Name        string         `json:"name" gorm:"not null;uniqueIndex;size:100"`
	Description string         `json:"description" gorm:"size:500"`
	Template    string         `json:"template" gorm:"type:text"`   // 提示模板
	Arguments   string         `json:"arguments" gorm:"type:jsonb"` // 参数定义
	Category    string         `json:"category" gorm:"size:50"`
	Tags        string         `json:"tags" gorm:"type:jsonb"`
	UsageCount  int64          `json:"usage_count" gorm:"default:0"`
	IsPublic    bool           `json:"is_public" gorm:"default:false"`
	CreatedBy   uint           `json:"created_by"`
	UpdatedBy   uint           `json:"updated_by"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`
}

// MCPSession MCP会话模型
type MCPSession struct {
	ID           uint           `json:"id" gorm:"primarykey"`
	SessionID    string         `json:"session_id" gorm:"not null;uniqueIndex;size:100"`
	ClientInfo   string         `json:"client_info" gorm:"type:jsonb"`  // 客户端信息
	ServerInfo   string         `json:"server_info" gorm:"type:jsonb"`  // 服务端信息
	Capabilities string         `json:"capabilities" gorm:"type:jsonb"` // 能力声明
	Status       string         `json:"status" gorm:"default:active"`   // active, inactive, closed, error
	LastPing     *time.Time     `json:"last_ping"`
	CreatedAt    time.Time      `json:"created_at"`
	UpdatedAt    time.Time      `json:"updated_at"`
	DeletedAt    gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联
	Executions []MCPExecution `json:"executions,omitempty" gorm:"foreignKey:SessionID"`
}

// MCPExecution MCP执行记录模型
type MCPExecution struct {
	ID          uint       `json:"id" gorm:"primarykey"`
	SessionID   uint       `json:"session_id"`
	ToolID      *uint      `json:"tool_id"`
	ResourceID  *uint      `json:"resource_id"`
	PromptID    *uint      `json:"prompt_id"`
	RequestType string     `json:"request_type" gorm:"not null;size:50"` // tool_call, resource_read, prompt_get
	Request     string     `json:"request" gorm:"type:jsonb"`
	Response    string     `json:"response" gorm:"type:jsonb"`
	Status      string     `json:"status" gorm:"default:success"` // success, error, timeout
	Error       string     `json:"error" gorm:"type:text"`
	Duration    int64      `json:"duration"` // 执行时长（毫秒）
	StartedAt   time.Time  `json:"started_at"`
	CompletedAt *time.Time `json:"completed_at"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`

	// 关联
	Session  MCPSession   `json:"session,omitempty" gorm:"foreignKey:SessionID"`
	Tool     *MCPTool     `json:"tool,omitempty" gorm:"foreignKey:ToolID"`
	Resource *MCPResource `json:"resource,omitempty" gorm:"foreignKey:ResourceID"`
	Prompt   *MCPPrompt   `json:"prompt,omitempty" gorm:"foreignKey:PromptID"`
}

// TableName 指定表名
func (MCPTool) TableName() string {
	return "mcp_tools"
}

func (MCPResource) TableName() string {
	return "mcp_resources"
}

func (MCPPrompt) TableName() string {
	return "mcp_prompts"
}

func (MCPSession) TableName() string {
	return "mcp_sessions"
}

func (MCPExecution) TableName() string {
	return "mcp_executions"
}
